using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Smled
{
    public partial class TileSetEditor : Form
    {
        private TileSet mSet;
        private Image gray;
        private bool changed;

        public TileSetEditor()
        {
            InitializeComponent();
            tileList.SelectedIndexChanged += new EventHandler(tileList_SelectedIndexChanged);
            //txtTitle.TextChanged += new EventHandler(txtTitle_TextChanged);

            

            //numID.ValueChanged += new EventHandler(numID_ValueChanged);

            
            this.Shown += new EventHandler(TileSetEditor_Shown);
            propertyGrid.Validated += new EventHandler(propertyGrid_Validated);
        }

        void propertyGrid_Validated(object sender, EventArgs e)
        {
            RefreshList();
            //tileList_SelectedIndexChanged(null, null);

        }

        void TileSetEditor_Shown(object sender, EventArgs e)
        {
            mSet = new TileSet();
            tileList.LargeImageList = new ImageList();
            tileList.LargeImageList.ImageSize = new Size(32, 32);
            tileList.LargeImageList.ColorDepth = ColorDepth.Depth32Bit;
            tileList.View = View.LargeIcon;

            gray = new Bitmap(32, 32);
            Graphics g = Graphics.FromImage(gray);
            g.Clear(Color.Gray);
            g.Flush();

            changed = false;
            RefreshList();
        }

        /*
        void numID_ValueChanged(object sender, EventArgs e)
        {
            if (tileList.SelectedItems.Count > 0)
            {
                Tile t = ((TileViewItem)tileList.SelectedItems[0]).ItemTile;
                //numID.Value = mSet.GetLessOrEqual((int)numID.Value);
                //t.ID = (int)numID.Value;
                changed = true;
            }
        }
        */
        /*
        void txtTitle_TextChanged(object sender, EventArgs e)
        {
            if (tileList.SelectedItems.Count > 0)
            {
                Tile t = ((TileViewItem)tileList.SelectedItems[0]).ItemTile;
                t.Name = txtTitle.Text;
                if (t.Name.Length == 0)
                {
                    t.Name = "default-name";
                }

                tileList.SelectedItems[0].Text = t.Name;
                changed = true;
            }
        }
        */
        void tileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tileList.SelectedItems.Count > 0)
            {
                Tile t = ((TileViewItem)tileList.SelectedItems[0]).ItemTile;
                /*
                txtTitle.Text = t.Name;
                numID.Value = t.ID;
                */
                pbTile.Image = t.TileImage;
                propertyGrid.SelectedObject = t;
            }
            else
            {
                //txtTitle.Text = "";
                //numID.Value = 0;
                propertyGrid.SelectedObject = null;
                pbTile.Image = null;
            }

        }

        private void cmdAdd_Click(object sender, EventArgs e)
        {
            Tile t = mSet.AddNewTile();
            TileViewItem li = new TileViewItem(t);
            tileList.Items.Add(li);
            tileList.FocusedItem = (ListViewItem)li;
            tileList.LargeImageList.Images.Add(t.ID.ToString(), gray);
            li.ImageKey = t.ID.ToString();
            changed = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (tileList.SelectedItems.Count > 0)
            {
                Tile t = ((TileViewItem)tileList.SelectedItems[0]).ItemTile;
                if (MessageBox.Show("Are you sure you want to delete the tile?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
                mSet.Set.Remove(t);
                tileList.Items.Remove(tileList.SelectedItems[0]);

                if (tileList.LargeImageList.Images.Keys.Contains(t.ID.ToString()))
                {
                    tileList.LargeImageList.Images.RemoveByKey(t.ID.ToString());
                }

                changed = true;
            }
        }

        private void cmdBrowse_Click(object sender, EventArgs e)
        {
            if (tileList.SelectedItems.Count > 0)
            {
                Tile t = ((TileViewItem)tileList.SelectedItems[0]).ItemTile;
                openFileDialog.ShowDialog(this);
                try
                {
                    Image img = Image.FromFile(openFileDialog.FileName);
                    t.TileImage = img;
                    pbTile.Image = t.TileImage;
                    
                    if (tileList.LargeImageList.Images.Keys.Contains(t.ID.ToString()))
                    {
                        tileList.LargeImageList.Images.RemoveByKey(t.ID.ToString());
                    }
                    tileList.LargeImageList.Images.Add(t.ID.ToString(), t.TileImage);
                    tileList.SelectedItems[0].ImageKey = t.ID.ToString();
                    changed = true;
                }
                catch (Exception)
                {
                    MessageBox.Show("Error loading image.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

        private bool Save()
        {
            DialogResult dr = setSaveDialog.ShowDialog();
            if (dr == DialogResult.Cancel) return false;

            mSet.SaveToFile(setSaveDialog.FileName);
            return true;
        }

        private bool CheckAndSave()
        {
            if (!changed) return false;
            if (mSet.Set.Count == 0) return false;
            DialogResult res = MessageBox.Show("Do you want to save the current tileset?", "Question", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
            if (res == DialogResult.Cancel) return true;

            if (res == DialogResult.Yes)
            {
                return !Save();
            }

            return false;
        }

        private void RefreshList()
        {
            tileList.Clear();
            foreach (Tile t in mSet.Set)
            {
                TileViewItem li = new TileViewItem(t);
                tileList.Items.Add(li);
                tileList.FocusedItem = (ListViewItem)li;
                if (t.TileImage != null)
                {
                    tileList.LargeImageList.Images.Add(t.ID.ToString(), t.TileImage);
                }
                else
                {
                    tileList.LargeImageList.Images.Add(t.ID.ToString(), gray);
                }
                li.ImageKey = t.ID.ToString();
            }
        }

        public TileSet CurrentSet
        {
            get { return mSet; }
            set { mSet = value; }
        }

        private void cmdOpenTileset_Click_1(object sender, EventArgs e)
        {
            if (!CheckAndSave())
            {
                if (setOpenDialog.ShowDialog() == DialogResult.Cancel) return;
                mSet = new TileSet(setOpenDialog.FileName);
                RefreshList();
            }
        }

        private void cmdNewTileset_Click_1(object sender, EventArgs e)
        {
            if (!CheckAndSave())
            {
                mSet = new TileSet();
                tileList.Items.Clear();
                pbTile.Image = null;
                //txtTitle.Text = "";
                //numID.Value = 0;
                propertyGrid.SelectedObject = null;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Save();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MainForm.Instance.MainView.Map.Set != null)
            {
                mSet = MainForm.Instance.MainView.Map.Set;
                RefreshList();
            }
        }

        private void tileList_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }
    }
}